package com.infocollect.project.api.service.impl;

import com.infocollect.common.utils.ServletUtils;
import com.infocollect.framework.security.LoginUser;
import com.infocollect.framework.security.service.TokenService;
import com.infocollect.project.api.entity.TappActEntity;
import com.infocollect.project.api.entity.TappActinsEntity;
import com.infocollect.project.api.entity.TappProcAuditEntity;
import com.infocollect.project.api.helper.CurrentHelp;
import com.infocollect.project.api.helper.JSONUtil;
import com.infocollect.project.api.mapper.*;
import com.infocollect.project.api.service.*;
import com.infocollect.project.api.service.*;
import com.infocollect.project.api.vo.CopyUserEntity;
import com.infocollect.project.api.vo.CurrentUserInfoVO;
import com.infocollect.project.api.vo.HandleProcessVO;
import com.infocollect.project.api.vo.ProcInstHandleEntity;
import com.infocollect.project.form.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * (TappActdatapermission)流程处理类
 *
 * @author makejava
 * @since 2019-12-25 10:32:28
 */
@Service("ProcessHandleService")
public class ProcessHandleServiceImpl implements IProcessHandleService {
    @Autowired
    private TokenService tokenService;
    @Resource
    private ITappProcessService tappProcessDao;
    @Resource
    private ITappActService tappActDao;
    @Resource
    private ITappActroutingService tappActroutingDao;
    @Resource
    private ITappPorcessinsService tappPorcessinsDao;
    @Resource
    private ITappActinsService tappActinsDao;
    @Resource
    private ITappUserService tappUserDao;
    @Resource
    private ITappFromDataService tappFromdataDao;
    @Resource
    private ITappActinsCommunicateService tappActinscommunicateDao;
    @Resource
    private ITappProcAuditService tappProcauditDao;
    @Resource
    private ITappRoleService tappRoleDao;
    ProcInstHandleEntity Instance ;
    @Override
    public R<Long> HandleProc(HandleProcessVO param)  {
        try {
            Instance= new ProcInstHandleEntity();
            ProcInstHandleEntityParser(param);
            if ("发起".equals(Instance.getActionname())) {
                if (Instance.getProcid() == null) {
                    return R.newError ("发起失败,找不到流程模板ID");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("发起失败,当前用户不能为空");
                }

                if (Instance.getCurrentactentity() == null) {
                    return R.newError("发起失败,未找到当前流程节点信息!");
                }
                AddProcAudit(Instance);
                RunProcess();
            } else if ("审批".equals(Instance.getActionname())) {
                if (Instance.getProcid() == null) {
                    return R.newError("审批失败,找不到流程模板ID");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("审批失败,找不到节点实例ID");
                }
                if (Instance.getProcinsid() == null) {
                    return R.newError("审批失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    throw new Exception("审批失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("审批失败,当前用户不能为空");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null || tappActins.getFstatus().equals(7)) {
                    return R.newError("审批失败,找不到节点信息或者正在处理中！");
                }
                if(tappActins.getFstatus()==0){
                    return R.newError("审批失败,可能已处理！");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                Instance.getTappPorcessins().setFstatus(7);
                tappPorcessinsDao.updateById(Instance.getTappPorcessins());
                AddProcAudit(Instance);
                RunProcess();
            } else if ("驳回".equals(Instance.getActionname())) {
                if (Instance.getProcid() == null) {
                    return R.newError("驳回失败,找不到流程模板ID");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("驳回失败,找不到节点实例ID");
                }
                if (Instance.getProcinsid() == null) {
                    return R.newError("驳回失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("驳回失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("驳回失败,当前用户不能为空");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null || tappActins.getFstatus().equals(7)) {
                    return R.newError("驳回失败,找不到节点信息或者正在处理中！");
                }
                if(tappActins.getFstatus()==0){
                    return R.newError("驳回失败,可能已处理！");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                Instance.getTappPorcessins().setFstatus(7);
                tappPorcessinsDao.updateById(Instance.getTappPorcessins());
                AddProcAudit(Instance);
                RunProcess();
            } else if ("暂存".equals(Instance.getActionname())) {
                if (Instance.getProcid() == null) {
                    return R.newError("暂存失败,找不到流程模板ID");
                }
                if (Instance.getFromid() == null) {
                    return R.newError("暂存失败,找不到表单模板ID");
                }
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.TempSave();
            } else if ("转办".equals(Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("转办失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("转办失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("转办失败,找不到节点实例ID");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null || tappActins.getFstatus().equals(7)) {
                    return R.newError("转办失败,找不到节点信息或者正在处理中！");
                }
                if(tappActins.getFstatus()==0){
                    return R.newError("转办失败,可能已处理！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("转办失败,当前操作用户不能为空");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                tappActins.setFstatus(7);
                tappActinsDao.updateById(tappActins);

                AddProcAudit(Instance);
                if (Instance.getRedrituser() == null || Instance.getRedrituser().equals("")) {
                    return R.newError("转办失败,转办人不能为空");
                }
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.TransferProcInst(tappActins);
            } else if ("撤销".equals(Instance.getActionname())) {
                ///将流程实例的状态为6 并创建一个草稿单
                if (Instance.getProcinsid() == null) {
                    return R.newError("撤销失败,找不到流程实例ID");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("撤销失败,当前用户不能为空");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("撤销失败,找不到流程实例信息或者正在处理中！");
                }
                Instance.getTappPorcessins().setFstatus(7);
                tappPorcessinsDao.updateById(Instance.getTappPorcessins());

                AddProcAudit(Instance);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.OperationProcInst(6);
            } else if ("废弃".equals(Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("废弃失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("废弃失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("废弃失败,当前用户不能为空");
                }
                Instance.getTappPorcessins().setFstatus(7);
                tappPorcessinsDao.updateById(Instance.getTappPorcessins());
                AddProcAudit(Instance);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.OperationProcInst(5);
            } else if ("催办".equals(Instance.getActionname())) {

            } else if ("跳转".equals(Instance.getActionname())) {
//            if (procInstanceEntity.NextActID == null)
//                throw new Exception("跳转失败,找不到下一节点ID");
                if (Instance.getProcinsid() == null) {
                    return R.newError("跳转失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7)||Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("跳转失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("跳转失败,当前用户不能为空");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("跳转失败,找不到节点实例ID");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null&&tappActins.getFstatus().equals(7) ) {
                    return R.newError("跳转失败,找不到节点信息或者正在处理中！");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                tappActins.setFstatus(7);
                tappActinsDao.updateById(tappActins);

                AddProcAudit(Instance);
            } else if ("终止".equals(Instance.getActionname())) {
                ///将流程实例的状态为4
                if (Instance.getProcinsid() == null) {
                    return R.newError("终止失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7) || Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("终止失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("终止失败,当前用户不能为空");
                }
                Instance.getTappPorcessins().setFstatus(7);
                tappPorcessinsDao.updateById(Instance.getTappPorcessins());
                AddProcAudit(Instance);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.OperationProcInst(4);
            }
            else if ("发起沟通".equals(Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("发起沟通失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7) || Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("发起沟通失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("发起沟通失败,当前用户不能为空");
                }
                if (Instance.copylist==null||Instance.copylist.isEmpty()) {
                    return R.newError("发起沟通失败,沟通人不能为空");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("发起沟通失败，找不到节点实例ID");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null || tappActins.getFstatus().equals(7)) {
                    return R.newError("发起沟通失败,找不到节点信息或者正在处理中！");
                }
                if(tappActins.getFstatus()==0){
                    return R.newError("发起沟通失败,可能已处理！");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                tappActins.setFstatus(7);
                tappActinsDao.updateById(tappActins);
                String ccNames="";
                for (CopyUserEntity cc:Instance.copylist) {
                    ccNames=(ccNames.equals("")?cc.getName():ccNames+"、"+cc.getName());
                }
                ///创建待办待阅信息 抄送邮件，待办待阅邮件，流程结束邮件
                String communicationTitle = "【" + Instance.getCureentuser().getRealaname() + "发起沟通:" +ccNames  + "】" + "备注：" + Instance.getApprovedDes();
                AddProcAudit(Instance, communicationTitle);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.StartCommunication();
            } else if ("回复沟通".equals(Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("回复沟通失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7) || Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("回复沟通失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("回复沟通失败,当前用户不能为空");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("回复沟通失败，找不到节点实例ID");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null ) {
                    return R.newError("回复沟通失败,找不到节点信息！");
                }
                AddProcAudit(Instance);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.AnswerCommunication();
            } else if ("取消沟通".equals( Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("取消沟通失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(7) || Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("取消沟通失败,找不到流程实例信息或者正在处理中！");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("取消沟通失败,当前用户不能为空");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("取消沟通失败，找不到节点实例ID");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                if (tappActins == null || tappActins.getFstatus().equals(7)) {
                    return R.newError("取消沟通失败,找不到节点信息或者正在处理中！");
                }
                if(!tappActins.getFapproversuser().equals(Instance.getCureentuser().getUserName())){
                    return R.newError("当前操作用户没有权限！");
                }
                tappActins.setFstatus(7);
                tappActinsDao.updateById(tappActins);
                AddProcAudit(Instance);
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.CancerCommunication();
            }
            else if ("抄送".equals(Instance.getActionname())) {
                if (Instance.getProcinsid() == null) {
                    return R.newError("抄送失败,找不到流程实例ID");
                }
                Instance.setTappPorcessins(tappPorcessinsDao.queryById(Instance.getProcinsid()));
                if (Instance.getTappPorcessins() == null || Instance.getTappPorcessins().getFstatus().equals(4)) {
                    return R.newError("抄送失败,找不到流程实例信息或者流程已结束！");
                }
                if (Instance.getActinsid() == null) {
                    return R.newError("抄送失败找不到节点实例ID");
                }
                if (Instance.copylist==null||Instance.copylist.isEmpty()) {
                    return R.newError("抄送失败,抄送人不能为空");
                }
                if (Instance.getCureentuser() == null) {
                    return R.newError("抄送失败,当前用户不能为空");
                }
                TappActinsEntity tappActins = tappActinsDao.queryById(Instance.getActinsid());
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.CCProcInst(tappActins);
            }
        } catch (Exception ex) {
            Instance.addErrorlog("处理流程发生错误！具体错误信息为：" + ex.getMessage() + "堆栈信息：" + ex.toString());
            return R.newError( ex.getMessage());
        }
        if (Instance.getErrorlog().size() > 0) {
            String errorstr=String.join(";", Instance.getErrorlog());
            System.out.println(errorstr);
            return R.newError(errorstr);
        }
        return R.newOk(Instance.getProcinsid());
    }


    //组装处理的实体
    void ProcInstHandleEntityParser(HandleProcessVO param) throws Exception {

        Instance.setActionname(param.getActionName());
        // Instance.setAction(processJson.getString("ActionType"));
        Instance.setProcid(param.getProcID());
        Instance.setFromid(tappProcessDao.queryById(Instance.getProcid()).getFromid());
        Instance.setActid(param.getActID());
        Instance.setProcinsid(param.getProcInsID());
        Instance.setActinsid(param.getActInsID());
        Instance.setFromdata(param.getFromdata());
        LoginUser userInfo = tokenService.getLoginUser(ServletUtils.getRequest());
        CurrentUserInfoVO currentUserInfo = new CurrentUserInfoVO();
        currentUserInfo.setUserId(userInfo.getUserId());
        currentUserInfo.setUserName(userInfo.getUserName());

        Instance.setCureentuser(currentUserInfo);
        Instance.setProcinsno(Instance.getFromdata().getString("Header_formNumber"));
        Instance.setProcinsname(Instance.getFromdata().getString("Header_formSubTitle"));
        Instance.setPrevActName(param.getPrevActName());
        Instance.setApplicantuser(Instance.getFromdata().getString("Header_applicantUserName"));
        Instance.setApplicantuserno(Instance.getFromdata().getString("Header_applicantUserID"));
        Instance.setProcname(param.getProcname());
        Instance.setFromdataJson(JSONUtil.objToJson(Instance.getFromdata()));
        String copyuserJson="";
        switch (param.getActionType()) {
            case "Start":
                Instance.setActionname("发起");
                Instance.setAction(1);
                Instance.setStartuserid(Instance.getCureentuser().getUserName());
                break;
            case "Save":
                Instance.setActionname("暂存");
                break;
            case "Approval":
                Instance.setActionname("审批");
                Instance.setAction(5);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "Sendback":
                Instance.setActionname("驳回");
                Instance.setAction(3);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "Tranfer":
                Instance.setActionname("转办");
                Instance.setAction(4);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                Instance.setRedrituser(param.getOperationInfo().getRedritUser());
                break;
            case "Repeal":
                Instance.setActionname("撤销");
                Instance.setAction(2);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "Abandon":
                Instance.setActionname("废弃");
                Instance.setAction(7);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "HastenWork":
                Instance.setActionname("催办");
                Instance.setAction(9);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "FlowSkip":
                Instance.setActionname("跳转");
                Instance.setAction(11);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());

                break;
            case "终止":
                Instance.setActionname("终止");
                Instance.setAction(12);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "StartQuery":
                Instance.setActionname("发起沟通");
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                copyuserJson= JSONUtil.objToJson(param.getOperationInfo().getCopyUserlist());
                Instance.copylist=  JSONUtil.jsonToObjList(CopyUserEntity.class,copyuserJson);
                break;
            case "AnswerQuery":
                Instance.setActionname("回复沟通");
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());

                break;
            case "CancelQuery":
                Instance.setActionname("取消沟通");
                break;
            case "CopySend":
                Instance.setActionname("抄送");
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                copyuserJson  = JSONUtil.objToJson(param.getOperationInfo().getCopyUserlist());
                Instance.copylist=  JSONUtil.jsonToObjList(CopyUserEntity.class,copyuserJson);
                break;
            case "Abandon_App":
                Instance.setActionname("废弃");
                Instance.setAction(7);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            case "Repeal_App":
                Instance.setActionname("撤销");
                Instance.setAction(2);
                Instance.setFapproversuser(Instance.getCureentuser().getUserName());
                Instance.setApprovedDes(param.getOperationInfo().getApprovedDes());
                break;
            default:
                break;
        }
        Instance.setCurrentactentity(tappActDao.queryById(Instance.getActid()));
        Instance.setTappActroutingList(tappActroutingDao.queryByActID(Instance.getActid()));

    }

    ///处理运行流程
    void RunProcess() throws Exception {
        ProcRouteComm procRouteComm = new ProcRouteComm(Instance, Instance.getAction());
        String nextActchartid = procRouteComm.Route();
        if (nextActchartid == null && nextActchartid.equals("")) {
            throw new Exception("路由解析未找到下一节点！");
        }
        Instance.setNextactchartid(nextActchartid);
        if (!nextActchartid.equals("F_End")) { ///下一节点为流程结束
            List<TappActEntity> actlist = tappActDao.queryByNodeIdAndProcid(nextActchartid,Instance.getProcid());
            if (actlist.isEmpty() || actlist.size() == 0) {
                throw new Exception("下一节点：" + nextActchartid + "在数据库中未找到！\r\n");
            }
            Instance.setNextactid(actlist.get(0).getFid());
            Instance.setNexact(actlist.get(0));
            ApproverParsing approverParsing = new ApproverParsing(Instance,tappUserDao,tappRoleDao);
            Instance.setApprovalobject(approverParsing.GetNextActApprover());
            if (Instance.getApprovalobject() == null && Instance.getApprovalobject().length() == 0) {
                throw new Exception("下一节点：" + Instance.getNexact().getFactname() + "审批人获取失败！\r\n");
            } else {
                ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
                processHelper.SaveProcInfo();
            }
        } else {
            ProcessHelper processHelper = new ProcessHelper(Instance, tappPorcessinsDao, tappFromdataDao, tappActinsDao, tappProcauditDao, tappActinscommunicateDao);
            processHelper.SaveProcInfo();
        }
    }

    ///处理自动跳过的流程
    void NewCheckNextApprovalObject() {
        boolean iscontinuousby = Instance.getCurrentactentity().getFiscontinuousby();
        boolean notiscontinuousby = Instance.getCurrentactentity().getFisnotcontinuousby();
        if (notiscontinuousby) {


        }

    }

    void AddProcAudit(ProcInstHandleEntity procInstanceEntity) {
        AddProcAudit(procInstanceEntity, procInstanceEntity.getApprovedDes());
    }

    ///新增审批记录
    void AddProcAudit(ProcInstHandleEntity procInstanceEntity, String approvedDes) {

        try {
            TappProcAuditEntity procAudi = new TappProcAuditEntity();
            procAudi.setFprocinsid(procInstanceEntity.getProcinsid());
            if (procInstanceEntity.getActinsid() != null) {
                procAudi.setFactinsid(procInstanceEntity.getActinsid());
                if (!"取消沟通，回复沟通".contains(procInstanceEntity.getActionname())) {
                    TappActinsEntity actInctEntity = tappActinsDao.queryById(procInstanceEntity.getActinsid());
                    if (actInctEntity != null)
                        procAudi.setFactcreatedate(actInctEntity.getFcreationdate()); //开封时间
                } else {
//                    var communicationEntity = CacheHelper.Instance.GetCommunicationState(procAudi.FActInsID).FirstOrDefault();
//                    if (communicationEntity != null)
//                    {
//                        procAudi.FActCreateDate = communicationEntity.FCreationDate;
//                    }
//                    else
//                    {
//                        procAudi.FActCreateDate = DateTime.Now;
//                    }
                }
            } else {
                procAudi.setFactcreatedate(new Date());
            }
            if (procInstanceEntity.getCurrentactentity() != null) {
                procAudi.setFactname(procInstanceEntity.getCurrentactentity().getFactname());

            } else {

            }
//            if (string.IsNullOrEmpty(procAudi.FActName))
//            {
//                var actins = CacheHelper.Instance.GetActIns(procInstanceEntity.ProcInsID.Value).OrderByDescending(x => x.FCreationDate).FirstOrDefault();
//                if (actins != null)
//                {
//                    procAudi.FActName = actins.FActName;
//                }
//            }
            procAudi.setFapprovelaction(procInstanceEntity.getActionname());
            procAudi.setFapproveldescr(approvedDes != null ? approvedDes : procInstanceEntity.getApprovedDes());

            procAudi.setFactopendate(new Date());// = DateTime.Now;//开始时间
            procAudi.setFactfishdate(new Date());// = DateTime.Now;//结束时间
            procAudi.setFapproversname(procInstanceEntity.getCureentuser().getUserName());
            procAudi.setFcreateby(procInstanceEntity.getCureentuser().getUserName());// = procInstanceEntity.CureentUserID;
            procAudi.setFcreationdate(new Date()); //= DateTime.Now;
            procAudi.setFlastupdatedate(new Date()); //= procAudi.FCreationDate;
            procInstanceEntity.getTappProcauditList().add(procAudi);
        } catch (Exception ex) {
            Instance.addErrorlog("新增审批记录报错！具体错误信息：" + ex.getMessage() + ", 具体堆栈信息：" + ex.toString());
        }

    }
}
